﻿#region License
// (c) Intergen.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TextGlow.Control.Packaging
{
  public class Package : Container, IPackage
  {
    public const string MainRelationshipType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
    public const string DocumentContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml";

    private readonly IContentGetter _contentGetter;
    private readonly ContentTypeHelper _contentTypeHelper;

    /// <summary>
    /// Gets the Silverlight downloader for the package.
    /// </summary>
    /// <value>The downloader.</value>
    public IContentGetter ContentGetter
    {
      get { return _contentGetter; }
    }

    /// <summary>
    /// Gets the content type helper for the package.
    /// </summary>
    /// <value>The content type helper.</value>
    public ContentTypeHelper ContentTypeHelper
    {
      get { return _contentTypeHelper; }
    }

    /// <summary>
    /// Gets the container URI.
    /// </summary>
    /// <value>The URI.</value>
    public override Uri Uri
    {
      get { return PackageUriHelper.PackageRootUri; }
    }

    public Package(IContentGetter contentGetter)
    {
      if (contentGetter == null)
        throw new ArgumentNullException("contentGetter");

      _contentGetter = contentGetter;
      _contentTypeHelper = new ContentTypeHelper(this);

      Uri relationshipUri = PackageUriHelper.GetPartRelationshipUri(Uri);

      PackageRelationshipCollection relationships = new PackageRelationshipCollection(this, null);

      foreach (PackageRelationship relationship in relationships)
      {
        Uri key = PackageUriHelper.ResolvePartUri(PackageUriHelper.PackageRootUri, relationship.TargetUri);

        IPackagePart part = CreatePart(key, relationship.RelationshipType);

        Parts.Add(relationship.Id, part);
      }

      IPackagePart mainDocumentPart = GetPart(PartType.MainDocument);

      if (mainDocumentPart == null)
        throw new Exception("Could not find main document part.");

      if (mainDocumentPart.ContentType != DocumentContentType)
        throw new Exception(string.Format("Main document type of {0}. Only {1} is currently supported.", mainDocumentPart.ContentType, DocumentContentType));
    }

    public override IPackagePart CreatePart(Uri partUri, string relationshipType)
    {
      return new PackagePart(partUri, PackagePart.GetPartType(relationshipType), this);
    }

    /// <summary>
    /// Gets content from the downloader using the given URI.
    /// </summary>
    /// <param name="contentUri">The content URI.</param>
    /// <returns></returns>
    public virtual string GetContent(Uri contentUri)
    {
      return _contentGetter.GetStringContent(PackageUriHelper.GetDownloaderPartName(contentUri));
    }

    /// <summary>
    /// Tests whether content with the given URI exists in the package downloader.
    /// </summary>
    /// <param name="contentUri">The content URI.</param>
    /// <returns></returns>
    public virtual bool ContentExists(Uri contentUri)
    {
      try
      {
        string tempResult = _contentGetter.GetStringContent(PackageUriHelper.GetDownloaderPartName(contentUri));
        return (tempResult != null);
      }
      catch (Exception)
      {
        return false;
      }
    }
  }
}